package com.love.netty.util;


import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.Entity;
import com.baomidou.mybatisplus.generator.config.builder.GeneratorBuilder;
import org.apache.commons.lang3.StringUtils;

import java.util.Scanner;

/**
 * @ClassName: GeneratorCodeUtils
 * @Description:
 * @Author: wuj
 * @Date: 2021-06-09 16:52
 **/
public class GeneratorCodeUtils {

    public static final String URL = "jdbc:mysql://121.36.230.26:3306/love?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=UTC&allowPublicKeyRetrieval=true";
    public static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
    public static final String USER_NAME = "root";
    public static final String PASSWORD = "GGYS#200119@seocoo";



    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        // 全局配置
        GlobalConfig.Builder gc = new GlobalConfig.Builder();
        String projectPath = System.getProperty("user.dir");
        gc.outputDir(projectPath + "/src/main/java");
        gc.author("wuj");
        gc.openDir(false);
        //实体属性 Swagger2 注解
        //builder.enableSwagger();

        // 数据源配置
        DataSourceConfig.Builder dsc = new DataSourceConfig.Builder(URL,USER_NAME,PASSWORD);





        // 包配置
        PackageConfig.Builder pc = new PackageConfig.Builder();
        //pc.setModuleName(scanner("模块名"));
        pc.parent("com.love.netty");
        pc.entity("model");
        pc.mapper("mapper");
        pc.service("service");
        pc.serviceImpl("service.impl");


        // 自定义配置
//        InjectionConfig cfg = new InjectionConfig() {
//            @Override
//            public void initMap() {
//                // to do nothing
//            }
//        };

        // 如果模板引擎是 freemarker
//        String templatePath = "/templates/mapper.xml.ftl";


        // 自定义输出配置
//        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
//        focList.add(new FileOutConfig(templatePath) {
//            @Override
//            public String outputFile(TableInfo tableInfo) {
//                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
//                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
//                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
//            }
//        });
        /*
        cfg.setFileCreate(new IFileCreate() {
            @Override
            public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
                // 判断自定义文件夹是否需要创建
                checkDir("调用默认方法创建的目录");
                return false;
            }
        });
        */
//        cfg.setFileOutConfigList(focList);
//        mpg.setCfg(cfg);

        // 配置模板
        //TemplateConfig.Builder templateConfig = new TemplateConfig.Builder();
        // 配置自定义输出模板
        //指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        //templateConfig.entity("templates/entity2.java");
        //templateConfig.mapperXml(null);
        //mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig.Builder strategy = new StrategyConfig.Builder();
        strategy.addInclude(scanner("表名，多个英文逗号分割").split(","));
        String moduleName = pc.build().getModuleName();
        System.out.println("moduleName:"+moduleName);
        strategy.addTablePrefix(moduleName);
        // 代码生成器(数据源)
        AutoGenerator mpg = new AutoGenerator(dsc.build());
        //全局配置
        mpg.global(gc.build());
        //包配置
        mpg.packageInfo(pc.build());
        //设置策略配置
        mpg.strategy(strategy.build());
        mpg.execute();
    }
}
